/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 */
package com.bmackay.vonsteuben.model.hdfs;

import com.bmackay.semweb.commons.modeling.services.models.parameter.ParameterQuery;
import com.bmackay.semweb.commons.decorators.jena.graph.query.GraphQuery;
import com.bmackay.vonsteuben.core.hdfs.FileSystemTasks;
import com.bmackay.vonsteuben.ontology.VonSteuben;
import com.bmackay.vonsteuben.ontology.triples.VonSteubenTriples;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import java.util.logging.Level;
import java.util.logging.Logger;

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 *     To be used until Charles Brooks model / ziba execution is completed
 *                    TEMPORARY MODEL
 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 */
public class HdfsModel {

    public void execute(Model model, Node subject) {

        FileSystemTasks fileSystemTasks = new FileSystemTasks();
        ParameterQuery parameterQuery = new ParameterQuery();
        Node cmd = GraphQuery.getObect(model, VonSteubenTriples.hasCmd(subject));

        if (cmd.getLiteralLexicalForm().equals("copyFromLocalFile")) {
            try {
                String source = parameterQuery.getParameterValueString(model, getSource(subject, model));
                String dest = parameterQuery.getParameterValueString(model, getTarget(subject, model));
                fileSystemTasks.copyFromLocalFile(source, dest);
            } catch (Exception ex) {
                Logger.getLogger(HdfsModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (cmd.getLiteralLexicalForm().equals("delete")) {
            try {
                String source = parameterQuery.getParameterValueString(model, getSource(subject, model));
                fileSystemTasks.delete(source);
            } catch (Exception ex) {
                Logger.getLogger(HdfsModel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private Node getSource(Node subject, Model model) {
        Triple srcTriple = Triple.create(subject, Node.createURI(VonSteuben.getURI() + "hasSrc"), Node.ANY);
        return GraphQuery.getObect(model, srcTriple);
    }

    private Node getTarget(Node subject, Model model) {
        Triple t = Triple.create(subject, Node.createURI(VonSteuben.getURI() + "hasTarget"), Node.ANY);
        return GraphQuery.getObect(model, t);
    }
}

/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 * All Rights Reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */